package com.ruoyi.common.utils.status;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 描述：某个对象(T)，在某个状态下(Status)，因为发生了某个事件(Event)，产生了变化(Consumer<T>)
 */
public abstract class StatusEngine<T, Status> {

    private final Map<Status, Map<Event, Consumer<T>>> table = new HashMap<>();

    public void run(T obj, Status status, Event event, boolean condition) {
        if (!condition) {
            Map<Event, Consumer<T>> eventConsumerMap = table.getOrDefault(status, defaultConsumer(obj));
            Consumer<T> consumer = eventConsumerMap.get(event);
            consumer.accept(obj);
        }
    }

    protected abstract Map<Event, Consumer<T>> defaultConsumer(T obj);

    public void add(StatusRecord<T, Status> ... records) {
        for (StatusRecord<T, Status> record : records) {
            add(record.getStatus(), record.getConsumer());
        }
    }

    public void add(Status status, Consumer<T> consumer) {
        if (status == null || consumer == null) {
            throw new IllegalArgumentException("参数不合法！");
        }
//        table.put(status, consumer);
    }

    public void delete(Status status) {
        table.remove(status);
    }

    @Data
    public static class StatusRecord<T, Status> {

        private Status status;

        public Consumer<T> consumer;

        public StatusRecord(Status status, Consumer<T> consumer) {
            this.status = status;
            this.consumer = consumer;
        }

    }

    public static class Event {

    }

}
